#include "tcc_astar.h"
#include <boost/bind.hpp>

Astar::AstarFinder* astar_new()
{
	return new Astar:: AstarFinder;
}

void astar_delete(Astar::AstarFinder* astar)
{
	astar->FreeAstarMap();
	delete astar;
}

void astar_set_hestimate(Astar::AstarFinder* astar, int hestimate)
{
	astar->SetHestimate(hestimate);
}

void astar_set_diagonal_movable(Astar::AstarFinder* astar, bool movable)
{
	astar->SetDiagonalMovable(movable);
}

void astar_set_heavy_diagonals(Astar::AstarFinder* astar, bool heavy)
{
	astar->SetHeavyDiagonals(heavy);
}

bool astar_init_map(Astar::AstarFinder* astar, int width, int height, Astar_IsMovable is_moveable, Astar_GetMoveCost get_move_cast)
{
	return astar->InitAstarMap(width, height, boost::bind(is_moveable, _1, _2), boost::bind(get_move_cast, _1, _2));
}

astar_path_node* astar_find_path(Astar::AstarFinder* astar, int start_x, int start_y, int end_x, int end_y, Astar::HeuristicFormula formula, int search_limit)
{
	Astar::Path path;
	if (Astar::ASTAR_SUCCESS == astar->Findpath(start_x, start_y, end_x, end_y, path, formula, search_limit))
	{
		astar_path_node* node = new astar_path_node[path.size()];
		if (NULL != node)
		{
			int size = path.size();
			for (int i = 0; i < size; ++i)
			{
				node[i].x = path.front().x;
				node[i].y = path.front().y;
				node[i].next = i < size - 1 ? &node[i + 1] : NULL;
				path.pop_front();
			}
			return node;
		}
	}
	return NULL;
}

void astar_delete_path(astar_path_node* node)
{
	delete[] node;
}